package com.yide.myworld.api_result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yide.myworld.exception.ApiException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用返回对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> {
    // 响应code
    private String responseCode;
    // 响应信息
    private String responseMessage;
    // 响应对象
    private T data;

    public static <T> CommonResult success(T data) {
        // 200写死，成功只允许只有一种声音，不允许多种
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> CommonResult success() {
        // 200写死，成功只允许只有一种声音，不允许多种
        return restResult(null, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    // 传递responseCode。成功只有一种，但是错误有N状态
    public static <T> CommonResult error(String responseMessage) {
        return restResult(null, "system fail", responseMessage);
    }
    public static <T> CommonResult error(String responseCode, String responseMessage) {
        return restResult(null, responseCode, responseMessage);
    }

    public static <T> CommonResult error(T data,String responseCode, String responseMessage) {
        return restResult(data, responseCode, responseMessage);
    }

    private static <T> CommonResult restResult(T data, String responseCode, String responseMessage) {
        CommonResult<T> commonResponse = new CommonResult<T>();
        commonResponse.setResponseCode(responseCode);
        commonResponse.setResponseMessage(responseMessage);
        commonResponse.setData(data);
        return commonResponse;
    }
    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IEnumCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> CommonResult<T> failed(IEnumCode errorCode,String message) {
        return new CommonResult<T>(errorCode.getCode(), message, null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 失败返回结果(data带数据的结果)
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IEnumCode errorCode, T data) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), data);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }


    /**
     * 获取data数据并且检查是否响应成功
     * 在服务间远程调用获取数据时使用
     *
     * @return
     */
    @JsonIgnore
    public T checkSuccessAndGetData(){
        // 如果响应码非200则远程调用时发生异常
        if (!"200".equals(responseCode)){
            throw new ApiException(ResultCode.FEIGN_ERROR);
        }
        return data;
    }

    @JsonIgnore
    public boolean checkSuccess(){
        // 如果响应码非200则远程调用时发生异常
        if (!"200".equals(responseCode)){
            throw new ApiException(ResultCode.FEIGN_ERROR);
        }
        return Boolean.TRUE;
    }

}
